package com.loe.mvp.util;

import android.content.res.Resources;
import android.util.DisplayMetrics;

/**
 * 固定Dpi适配方案
 */
public class DpiResources extends Resources
{
    private static int dpi = 0;
    private static boolean isHeight = false;

    public DpiResources(Resources resources)
    {
        super(resources.getAssets(), resources.getDisplayMetrics(), resources.getConfiguration());
    }

    @Override
    public DisplayMetrics getDisplayMetrics()
    {
        DisplayMetrics displayMetrics = super.getDisplayMetrics();
        if(dpi > 0)
        {
            initDpi(displayMetrics);
        }
        return displayMetrics;
    }

    public static void setDpi(int dpi)
    {
        setDpi(dpi, false);
    }

    public static void setDpi(int dpi, boolean isHeight)
    {
        DpiResources.dpi = dpi;
        DpiResources.isHeight = isHeight;
        DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
        initDpi(displayMetrics);
    }

    public static int getDpi()
    {
        return dpi;
    }


    private static void initDpi(DisplayMetrics displayMetrics)
    {
        if(isHeight)
        {
            displayMetrics.densityDpi = dpi;
            displayMetrics.density = displayMetrics.heightPixels * 1f / dpi;
        }else
        {
            displayMetrics.densityDpi = dpi;
            displayMetrics.density = displayMetrics.widthPixels * 1f / dpi;
        }
        displayMetrics.scaledDensity = displayMetrics.density;
    }
}